package com.cyj.dream.admin.controller.admin.system;

import com.alibaba.fastjson.JSONObject;
import com.cyj.dream.admin.model.system.SysDepartment;
import com.cyj.dream.admin.service.system.SysDepartmentService;
import com.cyj.dream.core.aspect.annotation.ResponseResult;
import com.cyj.dream.core.aspect.annotation.log.LogRecord;
import com.cyj.dream.core.aspect.enums.OperatorType;
import com.cyj.dream.core.constant.Constant;
import com.cyj.dream.core.constant.enums.ResultCode;
import com.cyj.dream.core.exception.BizException;
import com.cyj.dream.core.util.IsEmptyUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * @Description: 部门模块
 * @BelongsProject: fckj-restaurant
 * @BelongsPackage: com.fckj.fckjrestaurant.controller.admin.systems.sys
 * @Author: ChenYongJia
 * @CreateTime: 2021-06-10 14:44
 * @Email: chen87647213@163.com
 * @Version: 1.0
 */
@Slf4j
@RestController
@ResponseResult
@Api(value = "department", tags = "部门模块")
@RequestMapping(value = "/pc/department", name = "部门模块")
public class SysDepartmentController {

    @Autowired
    private SysDepartmentService sysDepartmentService;

    /**
     * 查询部门树
     *
     * @param sysDepartment 部门对象
     * @return java.util.Map<java.lang.String,java.lang.Object>
     * @date 2021/6/10 15:10
     * @author ChenYongJia
     * @version 1.0
     */
    @ApiOperation(value = "查询部门树", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sysDepartment", value = "部门对象", dataType = "SysDepartment", required = false)
    })
    @LogRecord(title = "查询部门树", operatetype = OperatorType.SELECT)
    @RequestMapping(value = "/selectSysDepartmentTree", name = "查询部门树", method = RequestMethod.POST)
    public Map<String, Object> selectSysDepartmentTree(@RequestBody SysDepartment sysDepartment) {
        log.info("进入查询部门树控制器，参数为：{}", JSONObject.toJSONString(sysDepartment));
        Map<String, Object> map = new HashMap<>(Constant.INITIAL_CAPACITY);
        if (IsEmptyUtils.isEmpty(sysDepartment)) {
            log.error("出现错误：查询部门模块树失败！参数为空");
            throw new BizException(ResultCode.PARAM_NOT_COMPLETE);
        }

        map.put("data", sysDepartmentService.selectSysDepartmentTree(sysDepartment));
        return map;
    }

    /**
     * 查询部门下拉选项
     *
     * @return java.util.Map<java.lang.String,java.lang.Object>
     * @date 2021/6/10 15:10
     * @author ChenYongJia
     * @version 1.0
     */
    @ApiOperation(value = "查询部门下拉选项", httpMethod = "GET")
    @LogRecord(title = "查询部门下拉选项", operatetype = OperatorType.SELECT)
    @RequestMapping(value = "/selectSysDepartmentList", name = "查询部门下拉选项", method = RequestMethod.GET)
    public Map<String, Object> selectSysDepartmentList() {
        log.info("进入查询部门下拉选项控制器");
        Map<String, Object> map = new HashMap<>(Constant.INITIAL_CAPACITY);

        map.put("data", sysDepartmentService.selectSysDepartmentList());
        return map;
    }



    /**
     * 根据部门id查询部门信息
     *
     * @param sysDepartmentId 部门id
     * @return java.util.Map<java.lang.String, java.lang.Object>
     * @date 2021-5-19 14:24:37
     * @author ChenYongJia
     * @version 1.0
     */
    @ApiOperation(value = "根据部门id查询部门信息", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sysDepartmentId", value = "部门id", dataType = "Long", required = true)
    })
    @LogRecord(title = "根据部门id查询部门信息", operatetype = OperatorType.SELECT)
    @RequestMapping(value = "/findBySysDepartmentId", name = "根据部门id查询部门信息", method = RequestMethod.GET)
    public Map<String, Object> findBySysDepartmentId(Long sysDepartmentId) {
        log.info("进入根据部门id查询部门信息方法控制器，参数为 sysDepartmentId：{}", sysDepartmentId);
        Map<String, Object> map = new HashMap<>(Constant.INITIAL_CAPACITY);
        if (IsEmptyUtils.isEmpty(sysDepartmentId)) {
            log.error("出现错误：方法执行失败失败！参数为空，请检查参数后重新提交！");
            throw new BizException(ResultCode.PARAM_NOT_COMPLETE);
        }

        map.put("info", sysDepartmentService.selectSysDepartmentBySysDepartmentId(sysDepartmentId));
        return map;
    }

    /**
     * 添加部门详情信息
     *
     * @param sysDepartment 部门对象
     * @return java.util.Map<java.lang.String, java.lang.Object>
     * @date 2021/4/21 10:44
     * @author ChenYongJia
     * @version 1.0
     */
    @ApiOperation(value = "添加部门详情信息", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sysDepartment", value = "部门对象", dataType = "SysDepartment", required = true)
    })
    @LogRecord(title = "添加部门详情信息", operatetype = OperatorType.INSERT)
    @RequestMapping(value = "/addSysDepartments", name = "添加部门详情信息", method = RequestMethod.POST)
    public Map<String, Object> addSysDepartments(@RequestBody SysDepartment sysDepartment) {
        log.info("进入修改部门详情信息方法控制器，参数为 sysDepartment：{}", JSONObject.toJSONString(sysDepartment));
        Map<String, Object> map = new HashMap<>(Constant.INITIAL_CAPACITY);
        this.checkData(sysDepartment);

        map.put("info", sysDepartmentService.addDepartmentInfo(sysDepartment));
        return map;
    }

    /**
     * 修改部门详情信息
     *
     * @param sysDepartment 部门对象
     * @return java.util.Map<java.lang.String, java.lang.Object>
     * @date 2021/4/21 10:44
     * @author ChenYongJia
     * @version 1.0
     */
    @ApiOperation(value = "修改部门详情信息", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sysDepartment", value = "部门对象", dataType = "SysDepartment", required = true)
    })
    @LogRecord(title = "修改部门详情信息", operatetype = OperatorType.UPDATE)
    @RequestMapping(value = "/updSysDepartments", name = "修改部门详情信息", method = RequestMethod.POST)
    public Map<String, Object> updSysDepartments(@RequestBody SysDepartment sysDepartment) {
        log.info("进入添加部门详情信息方法控制器，参数为 sysDepartment：{}", JSONObject.toJSONString(sysDepartment));
        Map<String, Object> map = new HashMap<>(Constant.INITIAL_CAPACITY);
        if(IsEmptyUtils.isEmpty(sysDepartment.getDreamId())){
            log.error("出现错误：方法执行失败失败！参数为空，请检查参数后重新提交！");
            throw new BizException(ResultCode.PARAM_NOT_COMPLETE);
        }

        this.checkData(sysDepartment);

        map.put("info", sysDepartmentService.updateDepartmentInfo(sysDepartment));
        return map;
    }

    /**
     * 删除部门详情信息
     *
     * @param sysDepartment 部门对象
     * @return java.util.Map<java.lang.String, java.lang.Object>
     * @date 2021/4/21 10:44
     * @author ChenYongJia
     * @version 1.0
     */
    @ApiOperation(value = "删除部门详情信息", httpMethod = "DELETE")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sysDepartment", value = "部门对象", dataType = "SysDepartment", required = true)
    })
    @LogRecord(title = "删除部门详情信息", operatetype = OperatorType.DELETE)
    @RequestMapping(value = "/delSysDepartments", name = "删除部门详情信息", method = RequestMethod.DELETE)
    public Map<String, Object> delSysDepartments(@RequestBody SysDepartment sysDepartment) {
        log.info("进入删除部门详情信息方法控制器，参数为 sysDepartment：{}", JSONObject.toJSONString(sysDepartment));
        Map<String, Object> map = new HashMap<>(Constant.INITIAL_CAPACITY);
        if(IsEmptyUtils.isEmpty(sysDepartment.getDreamId())){
            log.error("出现错误：方法执行失败失败！参数为空，请检查参数后重新提交！");
            throw new BizException(ResultCode.PARAM_NOT_COMPLETE);
        }

        map.put("info", sysDepartmentService.deleteDepartmentInfo(sysDepartment));
        return map;
    }

    /**
     * 用于部门信息参数校验
     *
     * @param sysDepartment 部门对象入参参数
     * @return boolean
     * @date 2021/6/17 15:43
     * @author ChenYongJia
     * @version 1.0
     */
    public boolean checkData(SysDepartment sysDepartment){
        if (IsEmptyUtils.isEmpty(sysDepartment.getSysDepartmentName()) || IsEmptyUtils.isEmpty(sysDepartment.getSysDepartmentPrincipalEmail())
                || IsEmptyUtils.isEmpty(sysDepartment.getSysDepartmentPrincipal()) || IsEmptyUtils.isEmpty(sysDepartment.getSysDepartmentPrincipalPhone())) {
            log.error("出现错误：方法执行失败失败！参数为空，请检查参数后重新提交！");
            throw new BizException(ResultCode.PARAM_NOT_COMPLETE);
        }else{
            return true;
        }
    }

}